﻿


/* ==============================================================================
 * 创 建 者：
 * 创建日期：
 * 功能描述：语言翻译
 

 * 修 改 人:DZH
 * 修改时间:2017-11-23
 * 修改描述：

       注意：
        1.依赖Jquery.cookie插件
        2.须设置翻译语言包
 * ==============================================================================*/

var translate = function () {

    //默认数据格式
    //var translateData = [{ Language:"", Key: '', Value: '' }, {Language:"", Key: '', Value: '' }];
    var translateData = $.extend({}, window.translateData);
    var language = '';
    //翻译
    translateText = function (rawText) {
        //console.log("language--" + language);
        if (translateData && rawText) {
            $.each(translateData, function (idx, ele) {
                if (ele.Language == language)
                {
                    var rgx = new RegExp("^(?:\\s)*"+ele.Key+"(?:\\s)*$", 'g');
                    rawText = new String(rawText).replace(rgx, ele.Value);
                }
            });
        }
        return rawText;
    };
    //翻译所有
    translateDOM = function (dom) {
        language = $.cookie("SelfLanguage");
//      console.log(language);
        if (!language)
        {
            console.log("UnKnow Translate Target Language!!");
            return;
        }
        if ($.type(dom) == "string") {
            var $html = $(dom);
            dom = $html[0];
        }
        this.translateSingleDOM(dom);
        var self = this;
        $(dom).find('*').each(function () {
            translateSingleDOM(this);
        });
        this.translatePlaceholder();
    };
    //翻译所有
    translateDOMByData = function (dom, Data) {
        setTranslateData(Data);
        this.translateSingleDOM(dom);
        var self = this;
        $(dom).find('*').each(function () {
            //self.translateSingleDOM(this);
            translateSingleDOM(this);
        });
    };
    //翻译单个DOM
    translateSingleDOM = function (domEle) {
        var nodes = domEle.childNodes;
        $.each(nodes, function (idx, node) {
            if (node.nodeType == 3) {
                node.nodeValue = translateText(node.nodeValue);
            }
        });
        if (nodes.length == 0) {
            domEle.value = translateText(domEle.value);
        }
    };
    //翻译placeholder
    translatePlaceholder = function () {
        $('[placeholder]').each(function () {
            this.placeholder = translateText(this.placeholder);
        });
    };
    //设置翻译数据
    setTranslateData = function (data) {
        if (data) {
            translateData = $.extend(translateData, data);
        }
    };

    //首次加载执行翻译
    $(function () {
    	setTimeout(function () {
    	    var language = $.cookie("SelfLanguage");
            if (translateData && language) {
                setTranslateData(translateData);
                translateDOM(document.body);
            }
        }, 50)
    });
    
   	//调用该方法翻译页面
    translatePage = function(){
        var language = $.cookie("SelfLanguage");
        if (translateData && language) {
            setTranslateData(translateData);
            translateDOM(document.body);
        }
    };

    //设置翻译目标语言
    //params: node 按钮节点
    window.SetTranslateLanguage= function SetTranslateLanguage(node) {
        var val = $(node).attr("value");
        //console.log(val);
        var language = $.cookie("SelfLanguage", val,{path: '/' });
        window.location.reload();
    }

    return {
        //设置翻译目标语言
        SetTranslateLanguage:SetTranslateLanguage,
        //翻译所有
        translateDOM: translateDOM,
        //翻译单个DOM
        translateSingleDOM: translateSingleDOM,
        //翻译placeholder
        translatePlaceholder: translatePlaceholder,
        //设置翻译数据
        setTranslateData: setTranslateData,
    };
}();
/*
var translateData;

$(function () {
    translateData = $.extend({}, window.translateData);

    window.onload = function () {
        setTimeout(function () {
            translate.setTranslateData(translateData);
            translate.translateDOM(document.body);
        }, 50)
    };

});
*/
